const app = getApp();
import qs from "qs";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rangeType: ['通用券', '新人券', '商品券', '品类券'],
    theme: app.theme,
    currPage: 1,
    perPage: 8,
    list: [],
    loading:false,
    isEmpty:false,
    ticketNum:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.info(app);
    // app.setShareId(options.shareid);
    // 修改主题
    // app.setTheme();
    this.setData({
      list:app.data.coupon,
      theme: app.theme
    })
    
  },
  couponList() {
    let that = this;
    let shop = app.functions.getLocalStorage("shop");
    let user = app.functions.getLocalStorage("user");
    if(that.data.loading==false)return;
    let params = {
      memberId: user.id,
      centreId: shop.centreId,
      offset: (that.data.currPage - 1) * that.data.perPage,
      limit: that.data.perPage
    };
    that.setData({
      loading:true
    })
    app.api.couponList(params, (response) => {
      const { rows, total } = response.data;
      that.setData({
        ticketNum:response.ticketNum
      })
      if (that.data.currPage == 1) {
        if (rows.length == 0) {
          that.setData({
            isEmpty:true
          })
        }
        that.setData({
          loading:false,
          list:rows
        })
      } else {
        that.setData({
          loading:false
        })
        that.setData({
          list:that.data.list.concat(rows) 
        })
      }
    }, (error) => {

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // Long ticketId,Long mid
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this;
    // let user = app.functions.getLocalStorage("user");
    // if(app.functions.isEmpty(user)){
    //   app.functions.islogin();
    //   return false;
    // }else{
    //   that.setData({
    //     loading:true,
    //     list:[]
    //   },()=>{
    //     that.couponList();
    //   })
    // }
    app.functions.hideToast();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list:app.data.coupon,
      loading:true,
      currPage: 1,
      // list:[]
    }, () => {
      wx.stopPullDownRefresh();
      // this.couponList();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      loading:true,
      currPage: parseInt(this.data.currPage) + 1
    }, () => {
      // this.couponList();
    })
  },

  getCoupon(e) {
    app.functions.showToast("领取成功!");
    return;
    let that = this;
    let index = e.currentTarget.dataset.index;
    let user = app.functions.getLocalStorage("user");
    let params={
      mId: user.id,
      id: that.data.list[index].id
    };
    app.api.createCoupon(params,(response)=>{
      wx.showModal({
        title: '提示',
        content: '领取成功，去查看我的优惠券',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {
            wx.navigateTo({
              url: '/pages_user/coupon_list/coupon_list?index=0'
            })
          } else {
            that.couponList();
          }
        }
      });
    },(error)=>{
      app.functions.showToast(error.msg);
    })

  },
  getShop(){
    wx.switchTab({
      url: '/pages/home/home'
    });
      
  },
  toShareCoupon(event){
    let that = this;
    let user=app.functions.getLocalStorage("user");
    let index=event.target.dataset.index;
    let params={
      ticketId:that.data.list[index].id,
      mid:user.id
    };
    if(that.data.list[index].isSend==1){
      app.api.addChangeTicketRecode(params,(response)=>{
        wx.navigateTo({
          url: '/pages_subpack/share_coupon/share_coupon?ticketId='+that.data.list[index].id
        });
      },(error)=>{

      })
    }else{
      wx.navigateTo({
        url: '/pages_subpack/share_coupon/share_coupon?ticketId='+that.data.list[index].id
      });
    }
  },
  replateShare(value,index) {
    return value.replace(/{price}/, this.data.list[index].amount).replace(/{company}/, app.globalData.company);
  },
  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage(event) {
    var that = this;
    if(event.from === 'button') {
      let index=event.target.dataset.index;
      let user=app.functions.getLocalStorage("user");
      let shop = app.functions.getLocalStorage("shop");
      let shareUserId=app.functions.getLocalStorage("shareUserId");
      let shareData=app.functions.getLocalStorage("share");
      let params={
        ticketId:that.data.list[index].id,
        mid:user.id
      };
      if(that.data.list[index].isSend==1){
        app.api.addChangeTicketRecode(params,(response)=>{
          console.info(response);
        },(error)=>{
  
        })
      }
      return {
        title: that.replateShare(shareData.powerSharingText,index),
        path: '/pages/home/home?shareTicketId='+user.id+'&ticketId='+that.data.list[index].id,
        imageUrl: shareData.powerSharing,
        success: res => {
          console.info(res);
          
        }
      };
    }else if(event.from === 'menu'){
      let user=app.functions.getLocalStorage("user");
      let shop = app.functions.getLocalStorage("shop");
      let shareUserId=app.functions.getLocalStorage("shareUserId");
      var url ='https://image.hnjg.jigon.cn/data/upload/images/20200629110915655.jpg';
      return {
        title: '【'+app.globalData.company+'领券中心】天天领超值购物券，购物更省钱',
        path: '/pages_user/coupon_center/coupon_center?shareid='+shareUserId,
        imageUrl: url,
        success: res => {}
      };
    }
  }
})